module.exports = {
  httpurl: 'https://****/locationservice/getLocation_v3/sendData',
  mapid: '',
  urlpath: '',
  openid: '',
  scanType: 1,
  version: '1.0.0',
  messagelist: [],
  beaconList: [],
  resultList: [],
  specbeaconList: [],
  angle: 0,
  mobileType:"android",
  showLog: false,
  set_scanType: function (type) {
    this.scanType = type
  },
  set_mapId: function (id) {
    this.mapid = id
  },
  set_openId: function (id) {
    this.openid = id
  },
  set_urlpath: function (url) {
    this.urlpath = url
  },
  set_showLog: function (log) {
    this.showLog = log
  },
  get_nowtime: function () {
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    if (this.showLog) {
      console.log("当前时间戳为：" + timestamp);
    }
    return timestamp
  },
  init_blue_devices: function () {
    var that = this;

    wx.openBluetoothAdapter({
      success: function (res) {
        that.addLog('初始化蓝牙适配器成功')
      },
      fail: function (res) {
        that.addLog('请打开蓝牙和定位功能')
        that.addLog(res.errMsg)
      }
    })
  },
  addLog: function (msg) {
    let that = this
    if (this.showLog) {
      console.log(msg)
    }
    that.messagelist.push(msg)
  },
  startScan: function () {
    let that = this
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log('xxxxx');
        that.addLog('start scanBeacon')
        wx.startCompass()
        wx.startAccelerometer()
        wx.onCompassChange(function (res) {
          //console.log("direction:" + res.direction)
          that.angle = res.direction
        })
        wx.getSystemInfo({
          success(res) {
            console.log(res.system)
            if(res.system.toUpperCase().indexOf('IOS') > -1){
              that.mobileType = "ios"
            }
          }
        })
        wx.startBeaconDiscovery({ //开始搜索附近的iBeacon设备
          uuids: ['00000000-0000-0000-0000-000000000006', 'fda50693-a4e2-4fb1-afcf-c6eb07647825'], //参数uuid
          success: function (res) {
            //console.log(res)
            wx.onBeaconUpdate(function (res) { //监听 iBeacon 设备的更新事件  
              //console.log(res)
              //封装请求数据 
              var beacons = res.beacons;
              for (var i = 0; i < beacons.length; i++) {
                var bleObj = {};
                bleObj.timestamp = that.get_nowtime();
                bleObj.rssi = that.specBeacon_ios(that.mapid, beacons[i].major, beacons[i].minor, beacons[i].rssi);
                bleObj.mac = that.mapid + "_" + beacons[i].major + "_" + beacons[i].minor;
                //bleObj.mac = beacons[i].major + "_" + beacons[i].minor;
                that.beaconList.push(bleObj);
              }
              //提交信标到服务器
              that.senddata_v2()
            });
          },
          fail: function (res) {
            //先关闭搜索再重新开启搜索,这一步操作是防止重复wx.startBeaconDiscovery导致失败
            that.stopSearchBeacom();
          }
        })
      },
      fail: function (res) {
        that.addLog('请打开蓝牙和定位功能')
        that.addLog(res.errMsg)
        console.log('yyyyy');
      }
    })



  },
  specBeacon_ios: function (mapid, major, minor, rssi) {
    let that = this;
    var bleObj = {};
    bleObj.mac = mapid + "_" + major + "_" + minor;
    bleObj.rssi = rssi
    if (rssi < 0) {
      let add = true
      for (let i = 0; i < that.specbeaconList.length; i++) {
        if (that.specbeaconList[i].mac == bleObj.mac) {
          that.specbeaconList[i].rssi = rssi
          add = false
          break
        }
      }
      if (add) {
        that.specbeaconList.push(bleObj)
      }
      return rssi
    } else {
      for (let i = 0; i < that.specbeaconList.length; i++) {
        if (that.specbeaconList[i].mac == bleObj.mac) {
          return that.specbeaconList[i].rssi
        }
      }
      return -100
    }
  },
  stopScan: function () {
    let that = this
    wx.stopCompass()
    wx.stopAccelerometer()
    wx.stopBeaconDiscovery({
      success: function () {
        that.addLog("蓝牙搜索停止")
      }
    })
  },
  stopSearchBeacom: function () {
    let that = this
    wx.stopBeaconDiscovery({
      success: function () {
        wx.stopCompass()
        wx.stopAccelerometer()
        that.startScan();
      }
    })
  },
  senddata_v2: function () {
    var that = this
    var sendstr = ""
    for (var i = that.beaconList.length - 1; i >= 0; i--) {
      var one = that.beaconList[i]
      sendstr += one.mac + "," + one.rssi + ";"
    }
    that.beaconList = []
    that.addLog("发送数据：" + sendstr)
    wx.request({
      url: that.httpurl + that.urlpath,
      method: 'post',
      data: JSON.stringify({
        "openid": that.openid,
        "beaconlist": sendstr,
        "angle": that.angle,
      }),
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res);
      },
      fail: function () {
        console.log('fail');
      }
    })
  },
}